<?php
declare (strict_types=1);

namespace app\api\controller\v1;

use app\api\helpers\Tree;
use app\api\helpers\v1\traits\SearchDataForModel;
use app\api\model\v1\AdminLog;
use app\api\model\v1\Admins;
use app\api\model\v1\CaseAssign;
use app\api\model\v1\CaseBack;
use app\api\model\v1\CaseDelay;
use app\api\model\v1\Collect;
use app\api\model\v1\Community;
use app\api\model\v1\GatherCompany;
use app\api\model\v1\OperateLog;
use app\api\model\v1\Rectification;
use app\BaseController;
use think\helper\Str;
use think\Request;

class CollectCon extends BaseController
{


    use SearchDataForModel;

    /**
     * Created by PhpStorm.
     * User: 刘海龙
     * Date: 2022/1/2
     * Time: 10:59
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * 采集列表
     */
    public function getCollectList()
    {
        $limit = $this->request->param('limit', 10);
        $map = [];
        if ($this->request->has('gather_company_id') && !empty($this->request->param('gather_company_id'))) {
            $map[] = ['gather_company_id', '=', $this->request->param('gather_company_id')];
        }
        if ($this->request->has('uid') && !empty($this->request->param('uid'))) {
            $map[] = ['uid', '=', $this->request->param('uid')];
        }
        if ($this->request->has('community_id') && !empty($this->request->param('community_id'))) {
            $map[] = ['community_id', '=', $this->request->param('community_id')];
        }
        if ($this->request->has('project_id') && !empty($this->request->param('project_id'))) {
            $map[] = ['project_id', '=', $this->request->param('project_id')];
        }
        if ($this->request->has('target_id') && !empty($this->request->param('target_id'))) {
            $map[] = ['target_id', '=', $this->request->param('target_id')];
        }
        if ($this->request->has('sample_id') && !empty($this->request->param('sample_id'))) {
            $map[] = ['sample_id', '=', $this->request->param('sample_id')];
        }
        if ($this->request->has('date_range') && !empty($this->request->param('date_range'))) {
            $date_range = $this->request->param('date_range');
            $map[] = ['create_time', 'between', "$date_range[0],$date_range[1]"];
        }
        $res = $this->search(new Collect(), $map, $limit, 'desc', ['community', 'sample', 'project', 'target', 'admin', 'unit'])->each(function ($item, $key) {
            $item['path_arr'] = json_decode($item['path'], true);
            $item['case_grade_name'] = Collect::$case_grade[$item['case_grade']] ?? '';
            $item['status_name'] = Collect::$is_status[$item['status']] ?? '';
            return $item;
        });
        return $this->response($res);
    }

    /**
     * Created by PhpStorm.
     * User: 刘海龙
     * Date: 2022/1/2
     * Time: 13:05
     * @param $id
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 删除采集信息
     */
    public function deleteCollect($id)
    {
        $res = Collect::find($id);
        if (is_null($res)) {
            return $this->response(403, '找不到该资源信息');
        }
        //->force()
        $res->delete();
        AdminLog::record('删除采集信息', ['id' => $id], $this->request->uid, $this->request->username);
        return $this->response();
    }

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2022/1/10 15:42
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 撤销案件
     */
    public function deleteCase()
    {
        $id = $this->request->param('id');
        $res = Collect::find($id);
        if (is_null($res)) {
            return $this->response(403, '找不到该资源信息');
        }
        if ($res->status !== Collect::IS_STATUS_ONE) {
            return $this->response(403, '案件已审核无法撤销');
        }
        $res->delete();
        AdminLog::record('小程序端删除采集信息', ['id' => $id], $this->request->wx_id, $this->request->wx_user_name);
        return $this->response();
    }

    /**
     * Created by PhpStorm.
     * User: 刘海龙
     * Date: 2022/1/1
     * Time: 19:55
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * 我的采集
     */
    public function getMyCollectList()
    {
        $limit = $this->request->param('limit', 10);
        $type = $this->request->param('type', 0);
        $map = [];
        if (intval($type) !== Collect::IS_STATUS_ZERO) {
            $map[] = ['status', '=', $type];
        }
        //获取整改管理员和超级管理员
        if (!$this->auth->isSuperAdmin($this->request->wx_id)) {
            $group_id_arr = $this->auth->getGroups($this->request->wx_id)->toArray(); //获取当前角色的id
            $new_string = '';
            foreach ($group_id_arr as $k => $v) {
                $new_string .= $v['name'] . ',';
            }
            $new_string = rtrim($new_string, ',');
            if (Str::contains($new_string, '整改管理员')) {
                $childrenList = $this->search->getChildrenIds($this->request->wx_unit_id, true);
                $map[] = array('unit_id', 'in', $childrenList);
            } else {
                $map[] = ['uid', '=', $this->request->wx_id];
            }
        }
        $res = Collect::with(['sample', 'project', 'target'])->where($map)->order('create_time desc')->paginate($limit)->each(function ($item, $key) {
            //图片json转数组
            $path_arr = json_decode($item['path'], true);
            //取图片数组的第一个
            $item['path_arr'] = reset($path_arr);
            //获取状态
            $item['status_name'] = Collect::$is_status[$item['status']];
            return $item;
        });
        return $this->response($res);
    }

    /**
     * Created by PhpStorm.
     * User: 刘海龙
     * Date: 2022/1/1
     * Time: 13:46
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 上传案件
     */
    public function saveGather()
    {
        $id = $this->request->param('id', 0);
        $data = $this->request->param();
        foreach ($data['path'] as $key => $v) {
            $data['path'][$key]['thumb'] = $v['url'];
        }
        $data['path'] = json_encode($data['path'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); //不转义
        $community = Community::find($data['community_id']);
        if (is_null($community)) {
            return $this->response(403, '暂无法找到社区');
        }
        $data['area_id'] = $community->area_id; //县区
        $data['street_id'] = $community->street_id; //街道
        $data['gather_company_id'] = $this->request->wx_unit_id; //单位
        $data['uid'] = $this->request->wx_id; //中间件
        if (intval($id) === 0) {
            $collect_id = Collect::create($data)->id;
        } else {
            $collect_id = $id;
            $res = Collect::find($id);
            $res->save($data);
        }
        //操作日志
        AdminLog::record(intval($id) === 0 ? '上传采集案件' : '编辑采集案件', $data, $this->request->wx_id, $this->request->wx_user_name);

        //采集日志
        OperateLog::operateRecord(intval($id) === 0 ? $this->request->wx_user_name . ':上传采集案件' : $this->request->wx_user_name . ':编辑采集案件', $this->request->wx_id, $this->request->wx_user_name, $collect_id, OperateLog::IS_COLLECT_ONE);
        return $this->response();
    }

    /**
     * Created by PhpStorm.
     * User: 刘海龙
     * Date: 2022/1/2
     * Time: 13:45
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 获取采集单位数据
     */
    public function getGatherCompany()
    {
        $res = GatherCompany::field('id as value,gather_company_title as label')->select();
        return $this->response($res);
    }

    /**
     * Created by PhpStorm.
     * User: 刘海龙
     * Date: 2022/1/2
     * Time: 13:54
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 获取采集用户
     */
    public function getCollectAdmin()
    {
        $res = Admins::field('id as value,username as label')->where('unit_id', '<>', 0)->select();
        return $this->response($res);
    }

    /**
     * Created by PhpStorm.
     * User: 刘海龙
     * Date: 2022/1/2
     * Time: 19:36
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 操作日志
     */
    public function getOperateLog()
    {
        $collect_id = $this->request->param('collect_id');
        $res = OperateLog::where(['collect_id' => $collect_id])->order('create_time desc')->select();
        return $this->response($res);
    }


    /**
     * Created by PhpStorm.
     * User: 刘海龙
     * Date: 2022/1/3
     * Time: 14:24
     * @return \think\response\Json
     * 采集统计
     */
    public function getStatisticsInfo()
    {
        $type = $this->request->param('type','wx');
        if ($type === 'pc'){
            $admin_id = $this->request->uid;
        }else{
            $admin_id = $this->request->wx_id;
        }
        if (is_null($admin_id)){
            $admin_id = 1; // 默认
        }
        if(!$this->auth->isSuperAdmin($admin_id)){
            $group_id_arr = $this->auth->getGroups($admin_id)->toArray(); //获取当前角色的id
            $new_string = '';
            foreach ($group_id_arr as $k => $v) {
                $new_string .= $v['name'] . ',';
            }
            $new_string = rtrim($new_string, ',');
            if (Str::contains($new_string, '整改')) {
                $str = "整改";
                $count = Rectification::field('id')->count(); //总
                $month = Rectification::field('id')->whereMonth('create_time')->count(); //本月
                $last_month = Rectification::field('id')->whereMonth('create_time', 'last month')->count(); //上月
                $week = Rectification::field('id')->whereWeek('create_time')->count(); //本周
                $last_week = Rectification::field('id')->whereWeek('create_time', 'last week')->count(); //上周
                $day = Rectification::field('id')->whereDay('create_time')->count(); //当天
                $yesterday = Rectification::field('id')->whereDay('create_time', 'yesterday')->count(); //昨天
            } else {
                $str = "采集";
                $count = Collect::field('id')->count(); //总
                $month = Collect::field('id')->whereMonth('create_time')->count(); //本月
                $last_month = Collect::field('id')->whereMonth('create_time', 'last month')->count(); //上月
                $week = Collect::field('id')->whereWeek('create_time')->count(); //本周
                $last_week = Collect::field('id')->whereWeek('create_time', 'last week')->count(); //上周
                $day = Collect::field('id')->whereDay('create_time')->count(); //当天
                $yesterday = Collect::field('id')->whereDay('create_time', 'yesterday')->count(); //昨天
            }
            $data[] = [
                'str' => $str,
                'count' => $count,
                'month' => $month,
                'last_month' => $last_month,
                'week' => $week,
                'last_week' => $last_week,
                'day' => $day,
                'yesterday' => $yesterday,
            ];
        } else {
            $zg_count = Rectification::field('id')->count(); //总
            $zg_month = Rectification::field('id')->whereMonth('create_time')->count(); //本月
            $zg_last_month = Rectification::field('id')->whereMonth('create_time', 'last month')->count(); //上月
            $zg_week = Rectification::field('id')->whereWeek('create_time')->count(); //本周
            $zg_last_week = Rectification::field('id')->whereWeek('create_time', 'last week')->count(); //上周
            $zg_day = Rectification::field('id')->whereDay('create_time')->count(); //当天
            $zg_yesterday = Rectification::field('id')->whereDay('create_time', 'yesterday')->count(); //昨天

            $count = Collect::field('id')->count(); //总
            $month = Collect::field('id')->whereMonth('create_time')->count(); //本月
            $last_month = Collect::field('id')->whereMonth('create_time', 'last month')->count(); //上月
            $week = Collect::field('id')->whereWeek('create_time')->count(); //本周
            $last_week = Collect::field('id')->whereWeek('create_time', 'last week')->count(); //上周
            $day = Collect::field('id')->whereDay('create_time')->count(); //当天
            $yesterday = Collect::field('id')->whereDay('create_time', 'yesterday')->count(); //昨天
            $cj_data [] = [

                'str' => '采集',
                'count' => $count,
                'month' => $month,
                'last_month' => $last_month,
                'week' => $week,
                'last_week' => $last_week,
                'day' => $day,
                'yesterday' => $yesterday,
            ];
            $zg_data[] =
                [
                    'str' => '整改',
                    'count' => $zg_count,
                    'month' => $zg_month,
                    'last_month' => $zg_last_month,
                    'week' => $zg_week,
                    'last_week' => $zg_last_week,
                    'day' => $zg_day,
                    'yesterday' => $zg_yesterday,

                ];
            $data = array_merge($zg_data, $cj_data);
        }
        return $this->response($data);
    }

    /**
     * Created by PhpStorm.
     * User: 刘海龙
     * Date: 2022/1/4
     * Time: 23:48
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 采集详情 根据id查找
     */
    public function collectDetail()
    {
        $collect_id = $this->request->param('collect_id');
        $res = Collect::with(['community', 'sample', 'project' => ['target'], 'target', 'admin'])->find($collect_id);
        if (is_null($res)) {
            return $this->response(403, '未找到该信息');
        }
        //图片json转数组
        $path_arr = json_decode($res['path'], true);
        $res['path_arr'] = $path_arr;
        //获取状态
        $res['status_name'] = Collect::$is_status[$res['status']];
        return $this->response($res->toArray());
    }

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2022/1/10 9:55
     * @return \think\response\Json
     * 案件交办
     */
    public function caseAssign()
    {
        $data = $this->request->param();
        $data['admin_id'] = $this->request->wx_id; //整改人
        $data['assigned_id'] = $data['assign_id']; //交办人
        $res = Rectification::find($data['rectification_id']);
        if (is_null($res)) {
            return $this->response(403, '未找到整改案件');
        }
        //更新整改表的状态
        $res->save([
            'admin_id' => $data['assign_id'], //更换交办人
            'type' => Rectification::IS_TYPE_ONE //交办状态当前还是未整改状态
        ]);
        CaseAssign::create($data); //入交办表
        //操作日志
        AdminLog::record('交办整改案件', $data, $this->request->wx_id, $this->request->wx_user_name);
        //采集日志
        OperateLog::operateRecord($this->request->wx_user_name . ':交办整改案件给【' . $data['assign_name'] . '】进行整改,原因:' . $data['desc'], $this->request->wx_id, $this->request->wx_user_name, $data['collect_id'], OperateLog::IS_COLLECT_NINE);
        return $this->response();
    }

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2022/1/10 14:00
     * @return \think\response\Json
     * 案件延期
     */
    public function caseDelay()
    {
        $data = $this->request->param();
        $data['admin_id'] = $this->request->wx_id; //整改人
        $data['delay_date'] = $data['delayDate']; //延期日期
        $res = Rectification::find($data['rectification_id']);
        if (is_null($res)) {
            return $this->response(403, '未找到整改案件');
        }
        //更新整改表的状态
        $res->save([
            'type' => Rectification::IS_TYPE_THREE //延期状态
        ]);
        //入库延期表
        CaseDelay::create($data); //入延期表
        //操作日志
        AdminLog::record($this->request->wx_user_name . ':申请延期整改案件,申请日期为:【' . $data['delayDate'] . '】,原因:' . $data['desc'], $data, $this->request->wx_id, $this->request->wx_user_name);
        //采集日志
        OperateLog::operateRecord($this->request->wx_user_name . ':申请延期整改案件,申请日期为:【' . $data['delayDate'] . '】,原因:' . $data['desc'], $this->request->wx_id, $this->request->wx_user_name, $data['collect_id'], OperateLog::IS_COLLECT_FIVE);
        return $this->response();
    }

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2022/1/11 14:32
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 案件退回 -- 暂时废除
     * 此接口是需要选择单位的
     */
    public function caseBack()
    {
        $data = $this->request->param();
        $data['admin_id'] = $this->request->wx_id; //退回人id
        $admin = Admins::field('id,username')->where(['unit_id' => $data['back_unit_id']])->select()->toArray();
        $new_arr = [];
        foreach ($admin as $key => $value) {
            $is_super = $this->auth->isSuperAdmin($value['id']);
            if (!$is_super) { //非超级管理员的时候
                $group_id_arr = $this->auth->getGroups($value['id'])->toArray(); //获取当前角色的id
                foreach ($group_id_arr as $k => $v) {
                    if ($v['name'] === '整改管理员') { //当角色为
                        $new_arr = $value;
                    }
                }
            }
        }
        if (empty($new_arr)) {
            return $this->response(403, '该单位未设置整改管理员，无法退回');
        } else {
            $data['back_user_id'] = $new_arr['id'];
            //更新整改表
            $res = Rectification::find($data['rectification_id']);
            if (is_null($res)) {
                return $this->response(403, '未找到整改案件');
            }
            //更新整改表的状态
            $res->save([
                'type' => Rectification::IS_TYPE_FIVE //退回状态
            ]);
            //新增退回表
            CaseBack::create($data);
            //操作日志
            AdminLog::record($this->request->wx_user_name . ':申请退回整改案件,原因:' . $data['desc'], $data, $this->request->wx_id, $this->request->wx_user_name);
            //采集日志
            OperateLog::operateRecord($this->request->wx_user_name . ':申请退回整改案件,原因:' . $data['desc'], $this->request->wx_id, $this->request->wx_user_name, $data['collect_id'], OperateLog::IS_COLLECT_SEVEN);
            return $this->response();
        }

    }

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2022/1/11 15:23
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * 我的退回列表 --- 单位管理员
     */
    public function getMyBackCase()
    {
        $limit = $this->request->param('limit', 10);
        $map = [];
        //当前用户筛选条件
        $map[] = array('back_user_id', '=', $this->request->wx_id);
        $res = $this->search(new CaseBack(), $map, $limit, 'desc', ['admin.unit'])->each(function ($item, $key) {
            $item['status_name'] = CaseBack::$is_status[$item['status']] ?? '';
            return $item;
        });
        return $this->response($res);
    }

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2022/1/11 16:09
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 接收案件
     */
    public function agreeCaseBack()
    {
        $data = $this->request->param();
        $back = CaseBack::find($data['back_id']);
        if (is_null($back)) {
            return $this->response(403, '未找到退回申请');
        }
        $res = Rectification::find($data['rectification_id']);
        if (is_null($res)) {
            return $this->response(403, '未找到整改案件');
        }
        //更新退回表
        $back->save([
            'status' => CaseBack::IS_STATUS_TWO
        ]);
        //更新整改表
        $res->save([
            'admin_id' => $this->request->wx_id,
            'unit_id' => $this->request->wx_unit_id,
            'type' => Rectification::IS_TYPE_ONE,
            'status' => Rectification::IS_STATUS_ONE
        ]);
        //操作日志
        AdminLog::record($this->request->wx_user_name . ':接收退回整改案件,原因:' . $data['desc'], $data, $this->request->wx_id, $this->request->wx_user_name);
        //采集日志
        OperateLog::operateRecord($this->request->wx_user_name . ':接收退回整改案件,原因:' . $data['desc'], $this->request->wx_id, $this->request->wx_user_name, $data['collect_id'], OperateLog::IS_COLLECT_EiGHT);
        return $this->response();
    }

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2022/1/11 16:21
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 单位管理员----退回上级
     */
    public function caseBackUpUnit()
    {
        $data = $this->request->param();
        $data['admin_id'] = $this->request->wx_id; //退回人
        $unit_id = $this->request->wx_unit_id; //单位

        $company = GatherCompany::where(['type' => GatherCompany::IS_TYPE_TWO])->select()->toArray();
        $arr = Tree::instance()->init($company, 'pid')->getParent($unit_id);

        if (empty($arr)) { //已经是最顶级了
            //此处为后台管理员接收
            //更新整改表
            $res = Rectification::find($data['rectification_id']);
            if (is_null($res)) {
                return $this->response(403, '未找到整改案件');
            }
            //更新整改表的状态
            $res->save([
                'type' => Rectification::IS_TYPE_SIX, //退回状态 --- 到达超级管理员手中
                'refuse_desc' => '退回原因:' . $data['desc']
            ]);
            if (array_key_exists('back_id', $data)) { //判断当前是否存在KEY back_id  该key是退回表的id 为更改当前状态的！
                $back = CaseBack::find($data['back_id']);
                if (is_null($back)) {
                    return $this->response(403, '未找到退回申请');
                }
                //更新退回表
                $back->save([
                    'status' => CaseBack::IS_STATUS_ONE
                ]);
            }
            //操作日志
            AdminLog::record($this->request->wx_user_name . ':申请退回整改到超级管理员手中,原因:' . $data['desc'], $data, $this->request->wx_id, $this->request->wx_user_name);
            //采集日志
            OperateLog::operateRecord($this->request->wx_user_name . ':申请退回整改到超级管理员手中,原因:' . $data['desc'], $this->request->wx_id, $this->request->wx_user_name, $data['collect_id'], OperateLog::IS_COLLECT_SEVEN);
            return $this->response();
        } else {
            $parent_unit = reset($arr);
            $admin = Admins::field('id,username')->where(['unit_id' => $parent_unit['id']])->select()->toArray();
            $new_arr = [];
            foreach ($admin as $key => $value) {
                $is_super = $this->auth->isSuperAdmin($value['id']);
                if (!$is_super) { //非超级管理员的时候
                    $group_id_arr = $this->auth->getGroups($value['id'])->toArray(); //获取当前角色的id
                    foreach ($group_id_arr as $k => $v) {
                        if ($v['name'] === '整改管理员') { //当角色为
                            $new_arr = $value;
                        }
                    }
                }
            }
            if (empty($new_arr)) {
                return $this->response(403, '该单位未设置整改管理员，无法退回');
            } else {
                $data['back_user_id'] = $new_arr['id'];
                $data['back_unit_id'] = $parent_unit['id'];
                $data['back_unit_name'] = $parent_unit['gather_company_title'];
                //更新整改表
                $res = Rectification::find($data['rectification_id']);
                if (is_null($res)) {
                    return $this->response(403, '未找到整改案件');
                }
                //更新整改表的状态
                $res->save([
                    'type' => Rectification::IS_TYPE_FIVE, //退回状态
                    'refuse_desc' => '退回原因:' . $data['desc']
                ]);
                if (array_key_exists('back_id', $data)) { //判断当前是否存在KEY back_id  该key是退回表的id 为更改当前状态的！
                    $back = CaseBack::find($data['back_id']);
                    if (is_null($back)) {
                        return $this->response(403, '未找到退回申请');
                    }
                    //更新退回表
                    $back->save([
                        'status' => CaseBack::IS_STATUS_TWO
                    ]);
                }
                //新增退回表
                CaseBack::create($data);
                //操作日志
                AdminLog::record($this->request->wx_user_name . ':申请退回整改案件,原因:' . $data['desc'], $data, $this->request->wx_id, $this->request->wx_user_name);
                //采集日志
                OperateLog::operateRecord($this->request->wx_user_name . ':申请退回整改案件,原因:' . $data['desc'], $this->request->wx_id, $this->request->wx_user_name, $data['collect_id'], OperateLog::IS_COLLECT_SEVEN);
                return $this->response();
            }
        }
    }

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2022/1/11 17:11
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 拒绝退回
     */
    public function refuseCaseBack()
    {
        $data = $this->request->param();
        $res = Rectification::find($data['rectification_id']);
        if (is_null($res)) {
            return $this->response(403, '未找到整改案件');
        }
        //更新整改表的状态
        $res->save([
            'type' => Rectification::IS_TYPE_ONE,
            'refuse_desc' => '拒绝退回原因:' . $data['refuse_desc']
        ]);
        $back = CaseBack::find($data['back_id']);
        if (is_null($back)) {
            return $this->response(403, '未找到退回申请');
        }
        //更新退回表
        $back->save([
            'status' => CaseBack::IS_STATUS_THREE,
            'refuse_desc' => '拒绝退回原因:' . $data['refuse_desc']
        ]);
        //找下级
        $back_two = CaseBack::where(['rectification_id' => $data['rectification_id'], 'collect_id' => $data['collect_id'], 'back_user_id' => $back->admin_id])->find();
        if (!is_null($back_two)) {
            $back_two->save([
                'status' => CaseBack::IS_STATUS_THREE,
                'refuse_desc' => '拒绝退回原因:' . $data['refuse_desc']
            ]);
        }
        //操作日志
        AdminLog::record($this->request->wx_user_name . ':拒绝退回申请,原因:' . $data['refuse_desc'], $data, $this->request->wx_id, $this->request->wx_user_name);
        //采集日志
        OperateLog::operateRecord($this->request->wx_user_name . ':拒绝退回申请,原因:' . $data['refuse_desc'], $this->request->wx_id, $this->request->wx_user_name, $data['collect_id'], OperateLog::IS_COLLECT_EiGHT);
        return $this->response();
    }

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2022/1/12 14:26
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 重新分配--超级管理员整改案件
     */
    public function reassignSave()
    {
        $data = $this->request->param();
        $res = Rectification::find($data['id']);
        if (is_null($res)) {
            return $this->response(403, '未找到该案件');
        }
        $res->save([
            'unit_id' => $data['unit_id'],
            'admin_id' => $data['admin_id'],
            'type' => Rectification::IS_TYPE_ONE,
            'status' => Rectification::IS_STATUS_ONE,
        ]);

        //同意退回后更新退回表状态
        $back = CaseBack::find($data['back_id']);
        if (is_null($back)) {
            return $this->response(403, '未找到该案件退回申请');
        }
        $back->save([
            'status' => CaseBack::IS_STATUS_TWO, //更新已处理
        ]);
        //操作日志
        AdminLog::record($this->request->username . ':同意退回申请', $data, $this->request->uid, $this->request->username);
        //采集日志
        OperateLog::operateRecord($this->request->username . ':同意退回申请', $this->request->uid, $this->request->username, $res['collect_id'], OperateLog::IS_COLLECT_EiGHT);
        return $this->response();
    }

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2022/1/12 14:38
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 查看退回表
     */
    public function getBackInfo()
    {
        $data = $this->request->param();
        $res = CaseBack::where([
            'rectification_id' => $data['rectification_id'],
            'collect_id' => $data['collect_id'],
            'status' => CaseBack::IS_STATUS_ONE
        ])->find();
        if (is_null($res)) {
            return $this->response(403, '未找到该退回信息');
        }
        return $this->response($res);
    }

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2022/1/12 14:47
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 拒绝退回---按照层级退回
     */
    public function refuseBackCase()
    {
        $data = $this->request->only(['id', 'back_id', 'collect_id', 'refuse_desc']);
        $res = Rectification::find($data['id']);
        if (is_null($res)) {
            return $this->response(403, '未找到该案件');
        }
        $res->save([
            'type' => Rectification::IS_TYPE_FIVE
        ]);

        //同意退回后更新退回表状态
        $back = CaseBack::find($data['back_id']);
        if (is_null($back)) {
            return $this->response(403, '未找到该案件退回申请');
        }
        $back->save([
            'status' => CaseBack::IS_STATUS_THREE, //更新已拒绝
            'refuse_desc' => '拒绝退回原因:' . $data['refuse_desc'],
        ]);
        //操作日志
        AdminLog::record($this->request->username . ':拒绝退回申请,原因:' . $data['refuse_desc'], $data, $this->request->uid, $this->request->username);
        //采集日志
        OperateLog::operateRecord($this->request->username . ':拒绝退回申请,原因:' . $data['refuse_desc'], $this->request->uid, $this->request->username, $data['collect_id'], OperateLog::IS_COLLECT_EiGHT);
        return $this->response();
    }

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2022/1/12 14:55
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 销案
     */
    public function cancelTheCase()
    {
        $data = $this->request->param();
        $res = Rectification::find($data['id']);
        if (is_null($res)) {
            return $this->response(403, '未找到该案件');
        }
        $res->save([
            'type' => Rectification::IS_TYPE_SEVEN
        ]);
        //删除当前整改案件
        $res->delete();
        //同意退回后更新退回表状态
        $back = CaseBack::find($data['back_id']);
        if (is_null($back)) {
            return $this->response(403, '未找到该案件退回申请');
        }
        $back->save([
            'status' => CaseBack::IS_STATUS_SEVEN, //更新已拒绝
        ]);
        $back->delete();
        //操作日志
        AdminLog::record($this->request->username . ':进行销案', $data, $this->request->uid, $this->request->username);
        //采集日志
        OperateLog::operateRecord($this->request->username . ':进行销案', $this->request->uid, $this->request->username, $data['collect_id'], OperateLog::IS_COLLECT_EiGHT);
        return $this->response();

    }

}
